package cn.mumzone.pos.presenter;


import java.util.ArrayList;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.pos.model.ExceptionEntity;
import cn.mumzone.pos.model.ExceptionReportEntity;
import cn.mumzone.pos.model.source.ReportRepository;
import cn.mumzone.pos.presenter.contract.ExceptionReportContract;

/**
 * Created by qiuzhenhuan on 2019/3/22.
 */

public class ExceptionReportPresenter extends AbsPresenter<ExceptionReportContract.View, ReportRepository> implements ExceptionReportContract.Presenter {

    @Override
    public void requestExceptionInfo(String startDate, String endDate) {
        mView.showLoadingDialog();
        ApiCallBack<ExceptionReportEntity> callBack = new ApiCallBack<ExceptionReportEntity>() {
            @Override
            public void onSuccess(ExceptionReportEntity entity) {
                ArrayList<ExceptionEntity> arrayList = new ArrayList<>();
                ExceptionReportEntity.GoodsReturnBean goodsReturnBean = entity.getGoodsReturn();
                if(goodsReturnBean!=null){
                    ExceptionEntity exceptionEntity = new ExceptionEntity();
                    exceptionEntity.setType(goodsReturnBean.getGoodsReturnName());
                    exceptionEntity.setAmount(goodsReturnBean.getGoodsReturnAmount());
                    exceptionEntity.setCount(goodsReturnBean.getGoodsReturnNum());
                    exceptionEntity.setReturnGainAmount(goodsReturnBean.getGoodsExchangeGainAmount());
                    exceptionEntity.setReturnLoseAmount(goodsReturnBean.getGoodsExchangeLostAmount());
                    arrayList.add(exceptionEntity);
                }
                ExceptionReportEntity.CardReturnBean cardReturn = entity.getCardReturn();
                if(goodsReturnBean!=null){
                    ExceptionEntity exceptionEntity = new ExceptionEntity();
                    exceptionEntity.setType(cardReturn.getCardReturnName());
                    exceptionEntity.setAmount(cardReturn.getCardReturnAmount());
                    exceptionEntity.setCount(cardReturn.getCardReturnNum());
                    exceptionEntity.setReturnGainAmount(cardReturn.getCardExchangeGainAmount());
                    exceptionEntity.setReturnLoseAmount(cardReturn.getCardExchangeLostAmount());
                    arrayList.add(exceptionEntity);
                }
                ExceptionReportEntity.MlBean mlBean = entity.getMl();
                if(goodsReturnBean!=null){
                    ExceptionEntity exceptionEntity = new ExceptionEntity();
                    exceptionEntity.setType(mlBean.getMlName());
                    exceptionEntity.setAmount(mlBean.getMlAmount());
                    exceptionEntity.setCount(mlBean.getMlNum());
                    exceptionEntity.setReturnGainAmount("");
                    exceptionEntity.setReturnLoseAmount("");
                    arrayList.add(exceptionEntity);
                }
                ExceptionReportEntity.BargainBean bargainBean = entity.getBargain();
                if(goodsReturnBean!=null){
                    ExceptionEntity exceptionEntity = new ExceptionEntity();
                    exceptionEntity.setType(bargainBean.getBargainName());
                    exceptionEntity.setAmount(bargainBean.getBargainAmount());
                    exceptionEntity.setCount(bargainBean.getBargainNum());
                    exceptionEntity.setReturnGainAmount("");
                    exceptionEntity.setReturnLoseAmount("");
                    arrayList.add(exceptionEntity);
                }

                ExceptionReportEntity.PresentBean presentBean = entity.getPresent();
                if(presentBean!=null){
                    ExceptionEntity presentEntity = new ExceptionEntity();
                    presentEntity.setType(presentBean.getPresentName());
                    presentEntity.setAmount(presentBean.getPresentAmount());
                    presentEntity.setCount(presentBean.getPresentNum());
                    presentEntity.setReturnGainAmount("");
                    presentEntity.setReturnLoseAmount("");
                    arrayList.add(presentEntity);
                }
                mView.showExceptionInfo(arrayList);
                mView.dismissLoadingDialog();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };

        mRepo.getExceptionReportByDate(startDate,endDate, callBack);
        addSubscrebe(callBack);
    }
}
